package com.skychyn.xml;

import java.util.ArrayList;
import java.util.Calendar;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class WeatherHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================
	String stringValue;
	int intValue;

	private ParsedWeatherDataSet myParsedWeatherDataSet = new ParsedWeatherDataSet();
	private ArrayList<ParsedWeatherDataSet> pwdsList;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ArrayList<ParsedWeatherDataSet> getParsedData() {
		return this.pwdsList;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.pwdsList = new ArrayList<ParsedWeatherDataSet>();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

		/**
		 * New FORECAST tag - must create new weather info record From this tag
		 * we also can get info about date
		 */
		if (localName.equals("FORECAST")) {

			myParsedWeatherDataSet = new ParsedWeatherDataSet();

			Calendar date = Calendar.getInstance();

			Log.d("NIORDDE", date.getTimeZone().getDisplayName());

			// Extract all attributes
			int day = Integer.parseInt(atts.getValue("day"));
			int month = Integer.parseInt(atts.getValue("month"))-1;
			int year = Integer.parseInt(atts.getValue("year"));
			int hour = Integer.parseInt(atts.getValue("hour"));
			int minute = 0; // Who cares?
			int second = 0;  // Who cares?

			date.set(year, month, day, hour, minute,second);

			myParsedWeatherDataSet.setForecastDate(date);
		}

		/**
		 * New PRESSURE tag - get info about min and max pressure
		 */
		else if (localName.equals("PRESSURE")) {

			myParsedWeatherDataSet.setMaxPressure(Integer.parseInt(atts
					.getValue("max")));
			myParsedWeatherDataSet.setMinPressure(Integer.parseInt(atts
					.getValue("min")));
		}

		/**
		 * New TEMPERATURE tag - get info about min and max temperature
		 */
		else if (localName.equals("TEMPERATURE")) {

			myParsedWeatherDataSet.setMaxTemperature(Integer.parseInt(atts
					.getValue("max")));
			myParsedWeatherDataSet.setMinTemperature(Integer.parseInt(atts
					.getValue("min")));
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		/**
		 * Closing temperature tag
		 */
		if (localName.equals("TEMPERATURE")) {
			// do nothing
		}

		/**
		 * Closing pressure tag
		 */
		else if (localName.equals("PRESSURE")) {
			// do nothing
		}

		/**
		 * Closing forecast tag - must add gathered information to list
		 */
		else if (localName.equals("FORECAST")) {

			pwdsList.add(myParsedWeatherDataSet);

		}
	}
}
